package com.zjh.photoshopstudy.app.user;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.zjh.photoshopstudy.Model.AppUserEntity;
import com.zjh.photoshopstudy.R;
import com.zjh.photoshopstudy.Utils.App;
import com.zjh.photoshopstudy.Utils.SharePreferencesUtils;

public class ChangePasswordActivity extends AppCompatActivity {
    EditText oldPWD;
    EditText newPWD;
    EditText confirmPWD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        oldPWD = findViewById(R.id.old_pwd);
        newPWD = findViewById(R.id.new_pwd);
        confirmPWD = findViewById(R.id.confirm_pwd);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button:
                doChange();
                break;
        }

    }

    private void doChange() {
        AppUserEntity userEntity = SharePreferencesUtils.getLoginUser(this);
        if (!oldPWD.getText().toString().trim().equals(userEntity.getuPassword())) {
            Toast.makeText(this, "原密码错误", Toast.LENGTH_SHORT).show();
        }else if (newPWD.getText().toString().trim().length()<6) {
            Toast.makeText(this, "密码不能小于6位", Toast.LENGTH_SHORT).show();
        } else if (!confirmPWD.getText().toString().trim().equals(newPWD.getText().toString().trim())) {
            Toast.makeText(this, "两次输入密码不一致", Toast.LENGTH_SHORT).show();
        } else {
            UserHttp.changePassword(this, userEntity.getId(), newPWD.getText().toString().trim());
        }
    }

    public void changeSuccess(String password) {
        AppUserEntity userEntity = SharePreferencesUtils.getLoginUser(this);
        userEntity.setuPassword(password);
        SharePreferencesUtils.saveLoginUser(userEntity, this);
        Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    public void changeFail() {
        Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
    }
}
